// import { hashHistory, browserHistory } from 'react-router';
// import Config from './Config';

/*--服务类AJAX--*/
function likeArray(obj) { return typeof obj.length == 'number' }
/*-工具类-*/
export default class Tool {
    constructor() { }
    /**
     * 页面跳转
     */
    static push(url) {
        if (url.indexOf('http') != -1) {
            window.location.href = url;
            return false;
        }
        hashHistory.push(url);
    }
    /**
     * 
     * @param agreement 协议
     * @param params 参数
     * @param callback 回调
     */
    static callNative(agreement: string, params = {}, callback: Function) {
        let u = navigator.userAgent;
        let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isAndroid) {
            var result = androidAction({ service_id: agreement, content: params ? params : {} });
            callback(JSON.parse(result));
        } else {
            setupWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler(agreement, params ? params : {}, function responseCallback(responseData) {
                    callback(JSON.parse(responseData));
                })
            })
        }
    }
    /**
     * 设置标题页
     */
    static setTitle(title) {
        document.title = title ? title : '';
        var mobile = navigator.userAgent.toLowerCase();
        if (/iphone|ipad|ipod/.test(mobile)) {
            var iframe = document.createElement('iframe');
            iframe.style.visibility = 'hidden';
            iframe.setAttribute('src', '');
            var iframeCallback = function () {
                setTimeout(function () {
                    iframe.removeEventListener('load', iframeCallback);
                    document.body.removeChild(iframe);
                }, 0);
            };
            iframe.addEventListener('load', iframeCallback);
            document.body.appendChild(iframe);
        }
    }
    /**
     * 多个对象合并
     */
    static assign(...args) {
        /*if(Object.assign){
         return Object.assign({},...args)
         }*/
        let from,
            target = args[0] || {},
            length = args.length;
        for (let i = 0; i < length; i++) {
            if ((from = args[i]) != null) {
                for (let key in from) {
                    target[key] = from[key];
                }
            }
        }
        return target;
    }

    static each(elements, callback) {
        let i, key;
        //如果是数组
        if (likeArray(elements)) {
            for (i = 0; i < elements.length; i++) {
                if (callback.call(elements[i], i, elements[i]) === false) return elements
            }
        } else {
            for (key in elements)
                if (callback.call(elements[key], key, elements[key]) === false) return elements
        }
        return elements
    }
    /**
     * 判断否个对象是否存在这个键值
     */
    static hasPrototype(object: {}, name: string) {

        return object.hasOwnProperty ? object.hasOwnProperty(name) : (name in object);

    }

    /**
     * 判断一个样式是否存在
     * @param ele
     * @param cls
     * @returns {RegExpMatchArray}
     */
    static hasClass(ele, cls) {
        return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
    }

    /**
     * 添加样式
     * @param ele
     * @param cls
     */
    static addClass(ele, cls) {
        if (!this.hasClass(ele, cls)) ele.className += " " + cls;
    }

    /**
     * 删除样式
     * @param ele
     * @param cls
     */
    static removeClass(ele, cls) {
        if (this.hasClass(ele, cls)) {
            var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
            ele.className = ele.className.replace(reg, ' ');
        }
    }

    /**
     * 添加删除样式
     * @param ele
     * @param cls
     */
    static toggleClass(ele, cls) {
        if (this.hasClass(ele, cls)) {
            this.removeClass(ele, cls);
        } else {
            this.addClass(ele, cls);
        }
    }

    /**
     * 获取?后面所有参数
     */
    static getUrlParams(): any {
        let url = window.location.search;
        let theRequest = new Object();
        if (url.indexOf("?") != -1) {
            let str = url.substr(1);
            let strs = str.split("&");
            for (let i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    /**
     * 简易的内置验证
     */
    static verifier(verifier) {
        let key = { isOk: true, tips: '' };
        verifier.forEach(function (item, index) {
            if (item.value.toString().trim().length <= 0) {
                key = { isOk: false, tips: item.tips };
                return false;
            }
        })
        return key;
    }

    //返回?后面指定的参数
    static getQueryString(Paras): any {
        return this.getUrlParams()[Paras]
    }

    //数组去重复
    static removeDuplicatedItem(ar):any {
      var ret = [];
      for (var i = 0, j = ar.length; i < j; i++) {
          if (ret.indexOf(ar[i]) === -1) {
              ret.push(ar[i]);
          }
      }
  
      return ret;
    }
}